import 'package:dsp_bt_audio/lang/my_i19n.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';

import '../common/dialog.dart';
import '../tool/assets.dart';

class DialogSetup extends DialogWidget {
  @override
  DialogSetupState createState() => DialogSetupState();
}

class DialogSetupState extends DialogWidgetState {
  List<String> get config_list {
    return List.generate(4, ($0) {
      return '${I19n.i.config} ${$0}';
    });
  }

  @override
  double get height {
    final select1 = controller.setting_config[1].values.first['select'].value;
    final select2 = controller.setting_config[2].values.first['select'].value;
    return 300 + (select1 ? config_list.length * 20 : 0) + (select2 ? config_list.length * 20 : 0);
  }

  @override
  // TODO: implement padding
  EdgeInsets get padding => EdgeInsets.only(left: 20, right: 20, bottom: 20, top: 0);
  @override
  Widget get child {
    return Obx(() {
      var list = controller.setting_config.asMap().entries.map(($0) => buildItem($0.key)).toList();
      final select1 = controller.setting_config[1].values.first['select'].value;
      final select2 = controller.setting_config[2].values.first['select'].value;
      if (select1) {
        list.insert(2, buildSetupList(1));
      }
      if (select2) {
        list.insert(3, buildSetupList(2));
      }
      return Container(
        color: Colors.transparent,
        child: Column(spacing: 10, children: list),
      );
    });
  }

  Widget buildSetupList(int index) {
    return Container(
      height: config_list.length * 20,
      padding: EdgeInsets.only(left: 20),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: config_list.asMap().entries.map(($0) {
          return Expanded(
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                splashColor: Color_theme.withAlpha(100),
                highlightColor: Color_theme.withAlpha(100),
                onTap: () => controller.saveOrUseConfig(index, $0.key, context),
                child: Align(alignment: Alignment.centerLeft, child: Text($0.value)),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  Widget buildItem(int index) {
    return Obx(() {
      final map = controller.setting_config[index];
      final img = map.values.first['img'];
      final text = map.keys.first;
      final select = map.values.first['select'].value;
      return Container(
        height: 30,
        child: Material(
          color: Colors.transparent,
          child: InkWell(
            borderRadius: BorderRadius.circular(5),
            onTap: () {
              setState(() {});
              controller.setupSelection(index, context);
            },
            splashColor: Color_theme.withAlpha(100),
            child: Padding(
              padding: EdgeInsetsGeometry.only(left: 10, right: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                spacing: 10,
                children: [
                  Image.asset(img, width: 16, height: 16),
                  Expanded(child: Text(text, textAlign: TextAlign.left)),
                  (index == 1 || index == 2) ? Image.asset(select ? assets.arrow_up : assets.arrow_down) : Container(),
                ],
              ),
            ),
          ),
        ),
      );
    });
  }
}
